package com.shen.unsafe;

import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;

public class SetTest {
    public static void main(String[] args) {
        /**
         * set不安全
         */
        //Set<Object> set = new HashSet<>();

        /**
         * Collections.synchronizedSet  将不安全的HashSet转换为安全的set  iterator需要手动同步
         */
        // Set<String> set =  Collections.synchronizedSet(new HashSet<>());

        /**
         * JUC方式
         */
        Set<String> set =   new CopyOnWriteArraySet();
        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                set.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(set);
            },String.valueOf(i)).start();
        }
        /**
         * Exception in thread "17" java.util.ConcurrentModificationException  并发修改错误
         */
    }
}
